$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyhomework/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
			{ label: '科目', name: 'subject', index: 'subject', width: 60 },
			{ label: '老师', name: 'teacher', index: 'teacher', width: 60 },
			{ label: '发布时间戳', name: 'creattime', index: 'creattime', width: 60 },
			{ label: '作业内容', name: 'worktext', index: 'worktext', width: 220 },
			{ label: '作业类型', name: 'workType', index: 'work_type', width: 60,formatter: function(item, index) {
                    if (item === 0) {
                        return '<span class="label label-warning">其他</span>';
                    }
                    if (item === 1) {
                        return '<span class="label label-success">家庭作业</span>';
                    }
                } },
			{ label: '班级', name: 'classId', index: 'class_id', width: 60,formatter: function(cellvalue, options, rowdata) {
                    var classname = '' ;
                    if(rowdata.classId!=null && rowdata.classId!=''){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyclasstable/info/"+rowdata.classId,
                            success: function(r){
                                classname = r.fyClassTable.classname;

                            }
                        });
                    }

                    return classname;
                } },
			{ label: '发布时间', name: 'creattimestr', index: 'creattimestr', width: 60 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		fyHomework: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyHomework = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.fyHomework.id == null ? "sys/fyhomework/save" : "sys/fyhomework/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyHomework),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyhomework/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/fyhomework/info/"+id, function(r){
                vm.fyHomework = r.fyHomework;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});